package com.aisino.nmg.util;

import com.aisino.nmg.constant.GeneratorConstants;

import java.io.*;

public class FileUtil {

	/**
	 * 在指定的文件路径上生成文件,并返回文件写入流
	 * @param filePath
	 * @param fileName
	 * @return
	 */
	public static Writer writeFileToPath(String filePath, String fileName){
		Writer writerOut = null;
		try {
			File newFile = new File(filePath, fileName);
			if(!newFile.exists()){
				newFile.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(newFile);
			writerOut = new BufferedWriter(new OutputStreamWriter(fos, GeneratorConstants.DEFAULT_ENCODING));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return writerOut;
	}

	/**
	 * 生成文件路径上的文件夹, 成功返回true, 失败返回false
	 * @param filePath
	 * @param fileName
	 */
	public static Boolean createFilePath(String filePath, String fileName){
		if (!filePath.endsWith(File.separator)) {
			filePath = filePath + File.separator;
		}

		File dir = new File(filePath);
		if (dir.exists()) {
			File file = new File(dir, fileName);
			if (file.exists()) {
				file.delete();
			}
			return true;
		}

		if (dir.mkdirs()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 生成文件路径上的文件夹, 成功返回true, 失败返回false
	 * @param filePath
	 */
	public static Boolean createFilePath(String filePath){
		if (!filePath.endsWith(File.separator)) {
			filePath = filePath + File.separator;
		}

		File dir = new File(filePath);
		if (dir.exists()) {
			return true;
		}

		if (dir.mkdirs()) {
			return true;
		} else {
			return false;
		}
	}
}
